package com.cskaoyan.servlet;

import com.cskaoyan.bean.MarketAd;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.service.AdminAdService;
import com.cskaoyan.service.AdminAdServiceImpl;
import com.cskaoyan.util.JacksonUtil;
import com.cskaoyan.util.StringUtil;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.SneakyThrows;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/admin/ad/*")
public class AdminAdServlet extends WdBaseServlet {
    AdminAdService adminAdService = new AdminAdServiceImpl();

    private BaseRespVo list(HttpServletRequest request, HttpServletResponse response) {
        String pageStr = request.getParameter("page");
        int page = Integer.parseInt(pageStr);

        String limitStr = request.getParameter("limit");
        int limit = Integer.parseInt(limitStr);

        String name = StringUtil.isEmpty(request.getParameter("name")) ? null : request.getParameter("name");
        String content = StringUtil.isEmpty(request.getParameter("content")) ? null : request.getParameter("content");


        String sort = request.getParameter("sort");
        String order = request.getParameter("order");

        BaseRespVo baseRespVo = adminAdService.list(page, limit, name, content, sort, order);

        return baseRespVo;
    }

    @SneakyThrows
    private BaseRespVo create(HttpServletRequest request, HttpServletResponse response) {
        // String createLine = request.getReader().readLine();
        // ObjectMapper objectMapper = new ObjectMapper();
        // JsonNode jsonNode = objectMapper.readTree(createLine);
        // String content = jsonNode.get("content").asText();
        // String enabled = jsonNode.get("enabled").asText();
        // String name = jsonNode.get("name").asText();
        // Integer position = jsonNode.get("position").asInt();
        // String url = jsonNode.get("url").asText();
        // // todo  还未实现图片上传 ,
        // // todo 能够上传 但是图片显示失败
        //
        // BaseRespVo baseRespVo = adminAdService.create(content, enabled, name, position, url);

        //2023年6月1日20:44:55
        String createLine = request.getReader().readLine();
        MarketAd marketAd = JacksonUtil.read(createLine, MarketAd.class);
        adminAdService.add(marketAd);
        return BaseRespVo.ok(marketAd);
    }

    @SneakyThrows
    private BaseRespVo update(HttpServletRequest request, HttpServletResponse response) {
        String updateLine = request.getReader().readLine();
        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode jsonNode = objectMapper.readTree(updateLine);
        // int id = jsonNode.get("id").asInt();
        // ObjectNode objectNode = (ObjectNode) jsonNode;
        // Date now = new Date();
        // SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // String nowStr = simpleDateFormat.format(now);
        // objectNode.put("updateTime",nowStr);
        // String string = objectMapper.writeValueAsString(objectNode);

        BaseRespVo baseRespVo = adminAdService.update(jsonNode);

        return baseRespVo;
    }

    @SneakyThrows
    private BaseRespVo delete(HttpServletRequest request, HttpServletResponse response) {

        //json
        String deleteLine = request.getReader().readLine();
        MarketAd readMarketAd = JacksonUtil.read(deleteLine, MarketAd.class);
        readMarketAd.setDeleted(true);
        BaseRespVo baseRespVo = adminAdService.delete(readMarketAd);

        return baseRespVo;
    }
}
